import React, { Component } from 'react';
import { Form, Row, Col, Button } from 'antd';
import ShowType from '../../../routes/common/showType';
import ReserveFields from '../../Common/ReserveFields/ReserveFields';
import styles from '../../../index.less';
import * as config from '../../../utils/config';

const FormItem = Form.Item;

class TabMainTableComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      sTabId: props.app.currentPane.key, /* 当前标签 */
      componentName: props.componentName, /* 组件名 */
      masterConfig: props.masterConfig, /* 配置 */
      sId: props.sId, /* 业务数据Id */
      dataList: [], /* 数据 */
      sActiveId: props.sActiveId, /* 窗体Id */
      disabled: props.disabled, /* 是否是查看状态 */
      sCustomerId: '', /* 客户(*) */
      sCustomerName: '', /* 客户名称(*)-伪字段 */
      sContacts: '', /* 联系人 */
      sPhone: '', /* 电话 */
      sManualNo: '', /* 手工单号 */
      sType: '', /* 类型 */
      sBillNo: '', /* 单据编号 */
      tCreateDate: '', /* 制单日期 */
      sMakePerson: '', /* 制单人 */
      sCurrency: '', /* 币别 */
      dCurrencyRate: '', /* 汇率 */
      sInvoiceNo: '', /* 发票号码 */
      sMinusNo: '', /* 红冲发票号码 */
      bAdvance: false, /* 预开票号 */
      sSalesManId: '', /* 销售员 */
      sGetPayId: '', /* 付款方式 */
      tGetPayDate: '', /* 结算日期 */
      sMemo: '', /* 备注 */
      dAccountReceivable: '', /* 应收款-伪字段 */
      dExpireReceivable: '', /* 到期应收-伪字段 */
      dTaxMoney: '', /* 税额-伪字段 */
      dTotalMoney: '', /* 合计-伪字段 */
    };
  }

  componentWillMount() {
    /* 获取主表数据 */
    const {
      masterConfig, sActiveId, sId, componentName, sTabId,
    } = this.state;
    const { dispatch } = this.props;
    if (sId !== undefined && sId !== '') {
      const mainId = masterConfig.sId;
      /* 表Id */
      const value = { sId, pageSize: '', pageNum: '' };
      /* 业务数据Id */
      const dataUrl = `${config.server_host}business/getBusinessDataByFormcustomId/${mainId}?sModelsId=${sActiveId}`;
      dispatch({
        type: 'content/getUrlPostData',
        payload: {
          url: dataUrl, sTabId, componentName, value, formRoute: '/salsalesinvoicemaster', returnType: 'data',
        },
      });
      // this.props.onFormData('CustomTable', dataUrl, value, []);
      /* 获取主表数据 */
    }
    if (masterConfig.gdsconfigformslave !== undefined) {
      const mainConfig = masterConfig.gdsconfigformslave;
      this.setState({
        sId: sId !== undefined ? sId : '',
        sCustomerId: mainConfig.filter(item => (item.sName.toLowerCase() === 'scustomerid'))[0],
        sCustomerName: mainConfig.filter(item => (item.sName.toLowerCase() === 'scustomername'))[0],
        sContacts: mainConfig.filter(item => (item.sName.toLowerCase() === 'scontacts'))[0],
        sPhone: mainConfig.filter(item => (item.sName.toLowerCase() === 'sphone'))[0],
        sManualNo: mainConfig.filter(item => (item.sName.toLowerCase() === 'smanualno'))[0],
        sType: mainConfig.filter(item => (item.sName.toLowerCase() === 'stype'))[0],
        sBillNo: mainConfig.filter(item => (item.sName.toLowerCase() === 'sbillno'))[0],
        tCreateDate: mainConfig.filter(item => (item.sName.toLowerCase() === 'tcreatedate'))[0],
        sMakePerson: mainConfig.filter(item => (item.sName.toLowerCase() === 'smakeperson'))[0],
        sCurrency: mainConfig.filter(item => (item.sName.toLowerCase() === 'scurrency'))[0],
        dCurrencyRate: mainConfig.filter(item => (item.sName.toLowerCase() === 'dcurrencyrate'))[0],
        sInvoiceNo: mainConfig.filter(item => (item.sName.toLowerCase() === 'sinvoiceno'))[0],
        sMinusNo: mainConfig.filter(item => (item.sName.toLowerCase() === 'sminusno'))[0],
        bAdvance: mainConfig.filter(item => (item.sName.toLowerCase() === 'badvance'))[0],
        sSalesManId: mainConfig.filter(item => (item.sName.toLowerCase() === 'ssalesmanid'))[0],
        sGetPayId: mainConfig.filter(item => (item.sName.toLowerCase() === 'sgetpayid'))[0],
        tGetPayDate: mainConfig.filter(item => (item.sName.toLowerCase() === 'tgetpaydate'))[0],
        sMemo: mainConfig.filter(item => (item.sName.toLowerCase() === 'smemo'))[0],
        dAccountReceivable: mainConfig.filter(item => (item.sName.toLowerCase() === 'daccountreceivable'))[0],
        dExpireReceivable: mainConfig.filter(item => (item.sName.toLowerCase() === 'dexpirereceivable'))[0],
        dTaxMoney: mainConfig.filter(item => (item.sName.toLowerCase() === 'dtaxmoney'))[0],
        dTotalMoney: mainConfig.filter(item => (item.sName.toLowerCase() === 'dtotalmoney'))[0],
      });
    }
  }

  componentWillReceiveProps(nextProps) {
    const { sId, disabled, content } = nextProps;
    const { dataList } = content;
    const {
      sDataListTabId, returnType, urlData, loading, componentName,
    } = dataList;
    if (this.state.sTabId === sDataListTabId && this.state.sTabId === nextProps.app.currentPane.key
      && this.state.componentName === componentName) {
      if (loading) return;
      if (returnType === 'data') {
        this.setState({
          dataList: urlData,
          sId: sId !== undefined ? sId : '',
        });
      }
    }
    this.setState({ disabled });
  }

  shouldComponentUpdate(nextProps, nextState) {
    const { componentName } = nextProps;
    const { sId } = this.props;
    if (sId === undefined) {
      return true;
    }
    let flag = true;
    if (nextState.disabled !== this.state.disabled) {
      return true;
    }
    if (this.state.sTabId === nextProps.app.currentPane.key &&
      this.state.componentName === componentName) {
      flag = true;
    } else {
      flag = false;
    }
    return flag;
  }

  render() {
    const halfformItemLayout = { labelCol: { span: 9 }, wrapperCol: { span: 13 } };
    const {
      app, form, handleModalPane, dataName, dataType, onClose,
    } = this.props;
    const {
      sCustomerId, sCustomerName, sContacts, sPhone, sManualNo, sMinusNo, sType,
    } = this.state;
    const {
      sBillNo, tCreateDate, sMakePerson, sCurrency, dCurrencyRate, sInvoiceNo, dExpireReceivable,
    } = this.state;
    const {
      sSalesManId, sGetPayId, tGetPayDate, sMemo, dAccountReceivable, dTaxMoney, dTotalMoney, bAdvance,
    } = this.state;
    const {
      dataList, sId, sActiveId, disabled, masterConfig,
    } = this.state;
    const childProps = {
      app,
      form,
      sActiveId,
      dataList: dataList[0],
      sId,
      disabled,
      handleModalPane,
      dataName,
      dataType,
      onClose,
    };
    const mainConfig = masterConfig.gdsconfigformslave;
    const sCustomerIdProps = { showConfig: sCustomerId, ...childProps };
    const sCustomerNameProps = { showConfig: sCustomerName, ...childProps };
    const sContactsProps = { showConfig: sContacts, ...childProps };
    const sPhoneProps = { showConfig: sPhone, ...childProps };
    const sManualNoProps = { showConfig: sManualNo, ...childProps };
    const sBillNoProps = { showConfig: sBillNo, ...childProps };
    const tCreateDateProps = { showConfig: tCreateDate, ...childProps };
    const sMakePersonProps = { showConfig: sMakePerson, ...childProps };
    const sCurrencyProps = { showConfig: sCurrency, ...childProps };
    const dCurrencyRateProps = { showConfig: dCurrencyRate, ...childProps };
    const sInvoiceNoProps = { showConfig: sInvoiceNo, ...childProps };
    const sMinusNoProps = { showConfig: sMinusNo, ...childProps };
    const sTypeProps = { showConfig: sType, ...childProps };
    const bAdvanceProps = { showConfig: bAdvance, ...childProps };
    const sSalesManIdProps = { showConfig: sSalesManId, ...childProps };
    const sGetPayIdProps = { showConfig: sGetPayId, ...childProps };
    const tGetPayDateProps = { showConfig: tGetPayDate, ...childProps };
    const sMemoProps = { showConfig: sMemo, ...childProps };
    const dAccountReceivableProps = { showConfig: dAccountReceivable, ...childProps };
    const dExpireReceivableProps = { showConfig: dExpireReceivable, ...childProps };
    const dTaxMoneyProps = { showConfig: dTaxMoney, ...childProps };
    const dTotalMoneyProps = { showConfig: dTotalMoney, ...childProps };
    const mainConfigProps = { mainConfig, ...childProps };
    return (
      <FormItem className={styles.searchForm}>
        <Row type="flex" justify="space-between">
          <Col span={7}>
            <ShowType required sName="sCustomerId" showDefaultName="客户(*)" {...sCustomerIdProps} />
            <ShowType required sName="sCustomerName" showDefaultName="客户名称(*)" {...sCustomerNameProps} />
            <ShowType sName="sContacts" showDefaultName="联系人" {...sContactsProps} />
            <ShowType sName="sPhone" showDefaultName="电话" {...sPhoneProps} />
            <ShowType sName="sManualNo" showDefaultName="手工单号" {...sManualNoProps} />
            <ShowType sName="sType" showDefaultName="类型" {...sTypeProps} />
            <ShowType sName="bAdvance" showDefaultName="预开票号" {...bAdvanceProps} />
            <ShowType sName="sSalesManId" showDefaultName="销售员" {...sSalesManIdProps} />
            <ShowType sName="sGetPayId" showDefaultName="付款方式" {...sGetPayIdProps} />
            <ShowType sName="tGetPayDate" showDefaultName="结算日期" {...tGetPayDateProps} />
            <ShowType sName="sMemo" textArea showDefaultName="备注(双击)" {...sMemoProps} />
          </Col>
          <Col span={7}>
            <ShowType sName="sBillNo" showDefaultName="单据号" {...sBillNoProps} />
            <ShowType sName="tCreateDate" showDefaultName="制单日期" {...tCreateDateProps} />
            <ShowType sName="sMakePerson" showDefaultName="制单人" {...sMakePersonProps} />
            <Row>
              <Col span={16}>
                <ShowType sName="sCurrency" showDefaultName="币别" {...sCurrencyProps} formItemLayout={halfformItemLayout} />
              </Col>
              <Col span={5}>
                <ShowType sName="dCurrencyRate" labelNo showDefaultName="汇率" {...dCurrencyRateProps} />
              </Col>
            </Row>
            <ShowType sName="sInvoiceNo" showDefaultName="发票号码" {...sInvoiceNoProps} />
            <ShowType sName="sMinusNo" showDefaultName="红冲发票号码" {...sMinusNoProps} />
            <ShowType sName="dAccountReceivable" showDefaultName="应收款" {...dAccountReceivableProps} />
            <ShowType sName="dExpireReceivable" showDefaultName="到期应收" {...dExpireReceivableProps} />
            <ShowType sName="dTaxMoney" showDefaultName="税额" {...dTaxMoneyProps} />
            <ShowType sName="dTotalMoney" showDefaultName="合计" {...dTotalMoneyProps} />
            <Row>
              <Col span={21}>
                <Button style={{ float: 'right' }}>分摊</Button>
              </Col>
            </Row>
          </Col>
          <Col span={7}>
            <ReserveFields {...mainConfigProps} />
          </Col>
        </Row>
      </FormItem>
    );
  }
}

export default TabMainTableComponent;
